package com.example.auth.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.annotations.*;
import com.example.auth.model.RoleMenuModel;
import com.example.auth.model.RoleModel;
import com.example.auth.model.vo.RoleMenuModelVo;
import com.example.auth.service.RoleMenuModelService;
import com.example.auth.service.RoleModelService;
import com.example.db.model.IPage;
import com.example.utils.Results;

import java.util.Date;
import java.util.List;

/**
 * @Classname RoleMenuModelController
 * @Description TODO
 * @Date 2022/7/31 10:11
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@RestController("/rolemenumodel")
public class RoleMenuModelController {

    @Autowired
    private RoleMenuModelService roleMenuModelService;

    @Autowired
    private RoleModelService roleModelService;

    @PostMapping(value = "/page",desc = "分页")
    public JSONObject pageRoleMenuController(@Param(value = "pageNum",desc = "页码",defalultValue = "1")Integer pageNum,
                                             @Param(value = "pageSize",desc = "条数",defalultValue = "10")Integer pageSize,
                                             @Param(value = "searchParams",required = false)JSONObject object
                                             ){
        IPage<RoleMenuModel> roleMenuModelIPage=roleMenuModelService.pageRoleMenuModel(pageNum,pageSize,object);
       return Results.successLayuiTable(roleMenuModelIPage);
    }


    @GetMapping(value = "/getrolemodelmennidlist",desc = "查询授权的菜单列表")
    public JSONObject getRoleMenuModelMenuIdListController(@Param(value = "roid")String roleid){
        List<Integer> integerList=roleMenuModelService.getRoleMenuModelMenuListByRoleId(roleid);
        return Results.success(integerList);
    }

    @PostMapping("/saveRoleMenuModel")
    public JSONObject saveRoleMenuModel(@RequestBody RoleMenuModelVo roleMenuModelVo){
        if(roleMenuModelVo == null){
            return Results.error("必要信息不得为空");
        }
        RoleModel roleModel=roleModelService.getRoleModelById(String.valueOf(roleMenuModelVo.getRoleId()));
        if(roleModel==null){
            return Results.success("角色信息不存在");
        }
        List<Integer> menuIds=roleMenuModelVo.getMenuIds();
        if(menuIds==null){
            return Results.error("信息不得为空");
        }
        List<Integer> menuIdOld=roleMenuModelService.getRoleMenuModelMenuListByRoleId(roleModel.getId());
        for (int i = 0; i < menuIdOld.size() ; i++) {
            Integer menuId = menuIdOld.get(i);
            if(!menuIds.contains(menuId)){
                //不包含则删除
                roleMenuModelService.deleteRoleMenuByRoleIdandMenuId(roleModel.getId(),menuId);
            }else {
                menuIds.remove(menuId);
            }
        }
        //新增操作
        for (int i = 0; i < menuIds.size() ; i++) {
            Integer menuId=menuIds.get(i);
            RoleMenuModel roleMenuModel=roleMenuModelService.getRoleMenuModelByRoleIdandMenuId(roleModel.getId(),menuId);
            if(roleMenuModel!=null){
                continue;
            }
            RoleMenuModel role=new RoleMenuModel();
            role.setMenuId(menuId);
            role.setRoId(roleModel.getId());
            role.setCreatedate(new Date());
            roleMenuModelService.saveRoleMenuModel(role);
        }
        return Results.success("保存成功");
    }
}
